package org.scc.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.scc.model.Article;

/**
 * 文章数据访问类
 * @author rzpeng
 *
 */
public class ArticleDao extends BaseDao{
	
	@Override
	protected String getMapper(String methodName) {
		return "org.scc.sqlmap.ArticleMapper." + methodName;
	}
	
	/**
	 * 添加文章
	 * article的articleType对象不能为空
	 * article的author对象不能为空
	 * @param article
	 */
	public void add(Article article){
		insert( getMapper("add"), article, "add article error");
	}
	
	/**
	 * 更新文章
	 * 只更新标题、内容和文章类型
	 * @param article
	 */
	public void update(Article article){
		update(getMapper("update"), article, "update article error");
	}
	
	/**
	 * 删除文章
	 * @param ids 文章的id串, 用","分割
	 */
	public void delete(String ids){
		delete(getMapper("delete"), ids, "delete article error");
	}
	
	/**
	 * 加载一篇文章
	 * 级联查询出文章的类型与作者
	 * @param id
	 * @return article
	 */
	public Article load(int id){
		return selectOne( getMapper("load"), id, "load article error");
	}
	
	/**
	 * 根据文章类型来查询类型的文章
	 * @param typeId
	 * @return
	 */
	public List<Article> findByType(int typeId){
		return selectList(getMapper("findByType"), typeId, "find articles by Type error");
	}
	
	/**
	 * 查询用户新建的文章
	 * @param userId
	 * @return
	 */
	public List<Article> findByPageUser(int userId, int rowBeginIndex, int rowCount){
		Map<String, Integer> param = new HashMap<String, Integer>();
		param.put("userId", userId);
		param.put("rowBeginIndex", rowBeginIndex);
		param.put("rowCount", rowCount);
		return selectList(getMapper("findPageByUser"), param, "find articles by User error");
	}
	
	public List<Article> findPageByType(int at, int rowBeginIndex, int rowCount){
		Map<String, Integer> param = new HashMap<String, Integer>();
		param.put("at", at);
		param.put("rowBeginIndex", rowBeginIndex);
		param.put("rowCount", rowCount);
		return selectList(getMapper("findPageByType"), param, "find articles by type error");
	}
	
	public int selectCountByUser(int userId){
		return selectOne(getMapper("selectCountByUser"), userId, "select user article count error");
	}

	public int selectCountByType(int at){
		return selectOne(getMapper("selectCountByType"), at, "select user article count error");
	}
	
	public List<Article> findIndexArticleByType(int at){
		return selectList(getMapper("queryIndex"), at, "find articles by User error");
	}
}
